<?php

class CouponApp extends BackendApp {

    var $_coupon_mod;
    var $_couponsn_mod;

    function __construct() {
        $this->CouponApp();
    }

    function CouponApp() {
        parent::BackendApp();
        $this->_coupon_mod = & m('coupon');
        $this->_couponsn_mod = & m('couponsn');
    }

    function index() {

        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'store_name',
                'equal' => 'like',
            ), array(
                'field' => 'coupon.start_time',
                'name' => 'start_time',
                'equal' => '>=',
                'handler' => 'gmstr2time',
            ), array(
                'field' => 'coupon.end_time',
                'name' => 'end_time',
                'equal' => '<=',
                'handler' => 'gmstr2time_end',
            ),
        ));
        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order, array('asc', 'desc'))) {
                $sort = 'end_time';
                $order = 'asc';
            }
        } else {
            $sort = 'end_time';
            $order = 'asc';
        }

        $page = $this->_get_page(20);
        $coupon_list = $this->_coupon_mod->find(array(
            'fields' => 'coupon.*,s.store_name,s.store_id',
            'conditions' => "1=1 " . $conditions,
            'join' => 'belong_to_store',
            'order' => "$sort $order",
            'limit' => $page['limit'],
            'count' => true,
        ));

        $page['item_count'] = $this->_coupon_mod->getCount();
        $this->_format_page($page);
        $this->assign('filtered', $conditions ? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);
        $this->assign('coupon_list', $coupon_list);
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->display('coupon.index.html');
    }
    function drop() {
        $coupon_id = isset($_GET['id']) ? intval($_GET['id']) : '';
        if (empty($coupon_id)) {
            $this->show_warning('no_coupon');
            return;
        }
        $coupon = $this->_coupon_mod->get($coupon_id);
        if (empty($coupon)) {
            $this->show_warning('no_coupon');
            return;
        }
        $this->_coupon_mod->drop($coupon_id);
        if ($this->_coupon_mod->has_error()) {
            $this->show_warning($this->_coupon_mod->get_error());
            return;
        }
        $this->_couponsn_mod->drop('coupon_id =' . $coupon_id);
        $this->show_message('drop_ok', 'back_list', 'index.php?app=coupon');
    }
    
    //异步修改数据
   function ajax_col()
   {
       $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
       $column = empty($_GET['column']) ? '' : trim($_GET['column']);
       $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
       $data   = array();
       if (in_array($column ,array('mall_recommended','sort_order')))
       {
           $data[$column] = $value;
           $this->_coupon_mod->edit($id, $data);
           if(!$this->_coupon_mod->has_error())
           {
               echo ecm_json_encode(true);
           }
       }
       else
       {
           return ;
       }
       return ;
   }

      function setting(){
        $page = $this->_get_page(20);
        $coupon_list = &m('coupon_setting')->find(array(
            'conditions' => "1=1 ",
            'limit' => $page['limit'],
            'count' => true,
        ));
        $page['item_count'] = &m('coupon_setting')->getCount();
        $this->_format_page($page);
        $this->assign('page_info', $page);
        $this->assign('coupon_list', $coupon_list);
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->display('coupon.setting.html');
   }

   function add_setting(){
        if(IS_POST){
            $postData = $_POST;
            if(!$postData['money'] || !$postData['name'] || $postData['money'] < 1 || $postData['expiry_time'] < 1){
                 $this->show_warning('请填写正确的数据！');
                return;
            }
            $data['money'] = $postData['money'];
            $data['type'] = 2;
            $data['name'] = $postData['name'];
            $data['expiry_time'] = $postData['expiry_time']*24*3600;
            $data['remark'] = $postData['remark'];
            $data['min_money'] = $postData['min_money'];
            $save = &m("coupon_setting")->add($data);
            if (!$save)
            {
                $this->show_warning('添加失败');
                return;
            }else{
                $this->admin_log("增加代金券配置[".$_POST['name']."]",1);
            }
            $this->show_message('添加成功',
                'back_list',        'index.php?app=coupon&act=setting');
        }else{
          $this->display('coupon.form.html');
        }
   }
   
   function view_setting(){
        ini_set('display_errors','on');
        error_reporting(E_ALL);
        $ad_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if(IS_POST){
            $postData = $_POST;
            if(!$postData['money'] || !$postData['name'] || $postData['money'] < 1 || $postData['expiry_time'] < 1){
                 $this->show_warning('请填写正确的数据！');
                return;
            }

            $data['money'] = $postData['money'];
            $data['name'] = $postData['name'];
            $data['expiry_time'] = $postData['expiry_time']*24*3600;
            $data['remark'] = $postData['remark'];
            $data['min_money'] = $postData['min_money'];
            $save = &m("coupon_setting")->edit($ad_id,$data);
         
            $this->admin_log("修改代金券配置[".$_POST['name']."]",1);
            $this->show_message('保存成功',
                'back_list',        'index.php?app=coupon&act=setting',
                '重新编辑',    'index.php?app=coupon&amp;act=view_setting&amp;id=' . $ad_id);

        }else{
            $find_data     = &m('coupon_setting')->find($ad_id);
            if (empty($find_data))
            {
                $this->show_warning('信息错误');
                return;
            }
            $ad    =   current($find_data);
            $ad['day'] = $ad['expiry_time'] /24/3600;
            $this->assign('ad',$ad);
            $this->display('coupon.form.html');
        }
   }
}

?>
